﻿using System;
using System.Reflection;

namespace Axial.Reflection
{
	public static class ReflectionHelper
	{
		public static object GetPropertyValue(this object obj, string property)
		{
			return obj.GetType().GetProperty(property).GetValue(obj, null);
		}

		public static T GetPropertyValue<T>(this object obj, string property)
		{
			return (T)obj.GetType().GetProperty(property).GetValue(obj, null);
		}

        public static MethodInfo GetMethod(this object obj, string method)
        {
            return obj.GetType().GetMethod(method);
        }

        public static MethodInfo GetMethod(this object obj, string method, params Type[] types)
        {
            return obj.GetType().GetMethod(method, types);
        }

        public static void CallMethod(this object obj, string method, params object[] parameters)
        {
            obj.GetType().GetMethod(method).Invoke(obj, parameters);
        }

		public static T CallMethod<T>(this object obj, string method, params object[] parameters)
		{
			return (T)obj.GetType().GetMethod(method).Invoke(obj, parameters);
		}

		public static bool HasMethod(this object obj, string method)
		{
			return obj.GetType().GetMethod(method) != null;
		}

		public static bool IsJSSafe(this Type type)
		{
			return type.GetCustomAttributes(typeof(JSSafeAttribute), false).Length > 0;
		}
	}
}
